package com.xianye.erp.service.order.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import com.xianye.erp.service.order.OrderManager;
import com.xianye.erp.util.DoubleUtil;
import com.xianye.erp.util.ExpressDateUtil;
import com.xianye.erp.util.PageData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;


@Service("orderAPI")
@Scope("prototype")
public class OrderAPI {
	protected Logger log = Logger.getLogger(OrderAPI.class);
	@Autowired
	private JdbcTemplate jdbc;
	@Resource(name = "orderService")
	private OrderManager orderService;
	@Autowired
	private ExpressOrderAPI expressOrderAPI;

	private final static Executor executor = Executors.newCachedThreadPool();//启用多线程
	private List<PageData> orderList = new ArrayList<>();
	private List<PageData> updateOrderList = new ArrayList<>();
	private List<PageData> buyerinfoList = new ArrayList<>();
	private List<PageData> childList = new ArrayList<>();
	private List<PageData> logisticinfos = new ArrayList<>();
	private List<PageData> productList = new ArrayList<>();
	private List<PageData> receiptaddressList = new ArrayList<>();
	private List<PageData> msgList = new ArrayList<>();
	private List<String> buyerinfoDel = new ArrayList<>();
	private List<String> childDel = new ArrayList<>();
	private List<String> logisticinfoDel = new ArrayList<>();
	private List<String> productDel = new ArrayList<>();
	private List<String> msgDel = new ArrayList<>();
	private List<String> receiptaddressDel = new ArrayList<>();
	public OrderAPI(){
		
	}

	
	
	public  void  operate(PageData store,JSONArray orders){
		String sql = "SELECT orderId,orderStatus from e_order where 1 = 1 and STORE_ID="+store.get("ID");
		HashMap<Long, String> map = new HashMap<>();
		SqlRowSet set = jdbc.queryForRowSet(sql);
		while(set.next()){
			map.put(set.getLong("orderId"), set.getString("orderStatus"));
		}
		JSONArray array = checkRepeat(orders, map);
		refresh(store,array);
		batchExucte();
	}
	public void operateException(PageData store,JSONArray orders){
		try{
			for (int i=0;i<orders.size();i++){
				JSONObject  dto = orders.getJSONObject(i);
				Long orderId = (Long) dto.get("order_id");
				JSONObject json = expressOrderAPI.getJSONOrderDetail(store,orderId);
				saveOther(json,dto);
			}
		}catch (Exception e){
			e.printStackTrace();
			log.error("处理异常订单时抛出异常信息：",e);
		}

	}
	public  JSONArray  checkRepeat(JSONArray orders, HashMap<Long, String> map){
		try {
			//1.array删除数据库中完成的数据
			log.info("去重前数据量："+ orders.size());
			log.info("去重前MAP数据量："+ map.size());
			JSONArray newOrder = new JSONArray();
			for(int i=0;i<orders.size();i++){
				JSONObject obj = orders.getJSONObject(i);
				Long orderId = obj.getLong("order_id");
				if(map.get(orderId)==null){
					obj.put("refreshStatus", 1);
					newOrder.add(obj);
				}else{
					if(!map.get(orderId).equals("FINISH")){
						obj.put("refreshStatus", 2);
						newOrder.add(obj);
					}
					//完成订单看是否有纠纷
					if(map.get(orderId).equals("FINISH")&&obj.getString("issue_status").equals("IN_ISSUE")){
						obj.put("refreshStatus", 2);
						newOrder.add(obj);
					}
					
				}				
			}
			log.info("去重后数据量："+ orders.size());
			return newOrder;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("去重时抛出异常信息：",e);
			return orders;
		}	
	}
	public  void  refresh(final PageData store,final JSONArray orders) {
		try {			
			if(orders!=null){
				final CountDownLatch latch = new CountDownLatch(orders.size());
				for(int i=0;i<orders.size();i++){
					final int j = i;
					executor.execute(new Runnable() {
						@Override
						public void run() {
							try {
								JSONObject  dto = orders.getJSONObject(j);
								Long orderId = (Long) dto.get("order_id");
								JSONObject json = expressOrderAPI.getJSONOrderDetail(store,orderId);
								dto.accumulate("detail", json);
							} catch (Exception e) {
								e.printStackTrace();
							}finally{								
								latch.countDown();
							}
						}
					});
				}				
				latch.await();
				for(int i=0;i<orders.size();i++){
					JSONObject dto = orders.getJSONObject(i);
					JSONObject json = dto.getJSONObject("detail");				
					if(json!=null && json.containsKey("id")){
						if (dto.getInt("refreshStatus")==2) {
							edit(json,dto);
						}else{
							save(store,json,dto);
						}
					}else{
						log.error("获取到的订单详情为空或者不包含id,订单id"+dto.get("id"));
					}					
				}				
			}			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("数据处理抛出异常信息：",e);
		}
	}
	public void  batchExucte(){
		try {			
			/**------------删除--------------**/
			System.out.println("buyerinfoDel:"+buyerinfoDel.size());
			if(buyerinfoDel.size()>0){
				orderService.BatchDelete_buyerinfo(buyerinfoDel);
				buyerinfoDel.clear();
			}
			System.out.println("receiptaddressDel:"+receiptaddressDel.size());
			if(receiptaddressDel.size()>0){
				orderService.batchDelete_receiptaddress(receiptaddressDel);
				receiptaddressDel.clear();
			}
			System.out.println("logisticinfoDel:"+logisticinfoDel.size());
			if(logisticinfoDel.size()>0){
				orderService.batchDelete_logisticinfolist(logisticinfoDel);
				logisticinfoDel.clear();
			}
			System.out.println("productDel:"+productDel.size());
			if(productDel.size()>0){
				orderService.batchDelete_product(productDel);
				productDel.clear();
			}
			System.out.println("childDel:"+childDel.size());
			if(childDel.size()>0){
				orderService.batchDelete_child(childDel);
				childDel.clear();
			}
			System.out.println("msgDel:"+msgDel.size());
			if(msgDel.size()>0){
				orderService.batchDelete_msglist(msgDel);
				msgDel.clear();
			}
			/**---------------添加-----------------**/
			System.out.println("orderList:"+orderList.size());
			if(orderList.size()>0){
				orderService.batchSave(orderList);
				orderList.clear();
			}
			System.out.println("buyerinfoList:"+buyerinfoList.size());
			if(buyerinfoList.size()>0){
				orderService.batchSave_buyerinfo(buyerinfoList);
				buyerinfoList.clear();
			}
			System.out.println("receiptaddressList:"+receiptaddressList.size());
			if(receiptaddressList.size()>0){
				orderService.batchSave_receiptaddress(receiptaddressList);
				receiptaddressList.clear();
			}
			System.out.println("childList:"+childList.size());
			if(childList.size()>0){
				orderService.batchSave_child(childList);
				childList.clear();
			}
			System.out.println("productList:"+productList.size());
			if(productList.size()>0){
				orderService.batchSave_product(productList);
				productList.clear();
			}
			System.out.println("logisticinfos:"+logisticinfos.size());
			if(logisticinfos.size()>0){
				orderService.batchSave_logisticinfolist(logisticinfos);
				logisticinfos.clear();
			}
			System.out.println("msgList:"+msgList.size());
			if(msgList.size()>0){
				orderService.batchSave_msglist(msgList);
				msgList.clear();
			}
			System.out.println("updateOrderList:"+updateOrderList.size());
			/**------------更新------------**/
			if(updateOrderList.size()>0){
				orderService.batchUpdate(updateOrderList);
				updateOrderList.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			updateOrderList.clear();
		}		
	}


	
	public void edit(JSONObject json,JSONObject  dto) throws Exception {
		try {
			PageData p = new PageData();
			p.put("orderId", json.get("id"));
			PageData pd = orderService.findByOrderId(p);
			if("FINISH".equals(pd.getString("orderStatus"))){
				return ;
			}
			pd.put("buyerSignerFullname", json.get("buyer_signer_fullname"));
			pd.put("buyerloginid", json.get("buyerloginid"));
			pd.put("frozenStatus", json.get("frozen_status"));
			pd.put("fundStatus", json.get("fund_status"));
			if(json.get("gmt_create")!=null){
				pd.put("gmtCreate", json.get("gmt_create"));
			}else{
				pd.put("gmtCreate", null);
			}
			if(json.get("gmt_modified")!=null){
				pd.put("gmtModified", json.get("gmt_modified"));
			}else{
				pd.put("gmtModified", null);
			}
			if(json.get("gmt_pay_success")!=null){
				pd.put("gmtPaySuccess", json.get("gmt_pay_success"));
			}else{
				pd.put("gmtPaySuccess",null);
			}
			if(json.get("gmt_trade_end")!=null){
				pd.put("gmtTradeEnd", json.get("gmt_trade_end"));
			}else{
				pd.put("gmtTradeEnd",null);
			}
			pd.put("orderId", json.get("id"));
			pd.put("isPhone", json.get("is_phone"));
			pd.put("issueStatus", json.get("issue_status"));
			pd.put("loanStatus", json.get("loan_status"));
			pd.put("logisticsStatus", json.get("logistics_status"));
			pd.put("orderStatus", json.get("order_status"));
			pd.put("paymentType", json.get("payment_type"));
			if(json.get("seller_operator_aliidloginid")!=null){
				pd.put("sellerOperatorAliidloginid", json.get("seller_operator_aliidloginid"));
			}else{
				pd.put("sellerOperatorAliidloginid", null);
			}
			pd.put("sellerOperatorLoginId", json.get("seller_operator_login_id"));
			pd.put("sellerSignerFullname", json.get("seller_signer_fullname"));
			JSONObject escrowFee = json.getJSONObject("escrow_fee");
			if(escrowFee!=null&&!escrowFee.isEmpty()&&escrowFee.toString().length()>2){
				pd.put("escrowFee_amount", escrowFee.get("amount"));
				pd.put("escrowFee_cent", escrowFee.get("cent"));
				pd.put("escrowFee_currencyCode", escrowFee.get("currency_code"));
			}else{
				pd.put("escrowFee_amount", null);
				pd.put("escrowFee_cent", null);
				pd.put("escrowFee_currencyCode", null);
			}
			JSONObject initOderAmount = json.getJSONObject("init_oder_amount");
			if(initOderAmount!=null&&!initOderAmount.isEmpty()&&initOderAmount.toString().length()>2){
				pd.put("initOderAmount_amount", DoubleUtil.getRes(initOderAmount.getDouble("cent"),100.00));
				pd.put("initOderAmount_cent", initOderAmount.get("cent"));
				pd.put("initOderAmount_currencyCode", initOderAmount.get("currency_code"));
			}else {
				pd.put("initOderAmount_amount", null);
				pd.put("initOderAmount_cent", null);
				pd.put("initOderAmount_currencyCode", null);
			}
			JSONObject loanInfo = json.getJSONObject("loan_info");
			if(loanInfo!=null&&!loanInfo.isEmpty()&&loanInfo.toString().length()>2){
				if(json.get("loanTime")!=null){
					pd.put("loanTime", ExpressDateUtil.parse(json.get("loan_time")+""));
				}else{
					pd.put("loanTime",null);
				}
				JSONObject loanAmount = loanInfo.getJSONObject("loan_amount");
				if(initOderAmount!=null&&!initOderAmount.isEmpty()&&initOderAmount.toString().length()>2){
					pd.put("loanAmount_amount", DoubleUtil.getRes(loanAmount.getDouble("cent"),100.00));
					pd.put("loanAmount_cent", loanAmount.get("cent"));
					pd.put("loanAmount_currencyCode", loanAmount.get("currency_code"));
				}else{
					pd.put("loanAmount_amount", null);
					pd.put("loanAmount_cent", null);
					pd.put("loanAmount_currencyCode", null);
				}
			}else{
				pd.put("loanTime",null);
				pd.put("loanAmount_amount", null);
				pd.put("loanAmount_cent", null);
				pd.put("loanAmount_currencyCode", null);
			}
			JSONObject logisticsAmount = json.getJSONObject("logistics_amount");
			if(logisticsAmount!=null&&!logisticsAmount.isEmpty()&&logisticsAmount.toString().length()>2){
				pd.put("logisticsAmount_amount", logisticsAmount.get("amount"));
				pd.put("logisticsAmount_cent", logisticsAmount.get("cent"));
				pd.put("logisticsAmount_currencyCode", logisticsAmount.get("currency_code"));
			}else{
				pd.put("logisticsAmount_amount", null);
				pd.put("logisticsAmount_cent", null);
				pd.put("logisticsAmount_currencyCode",null);
			}
			JSONObject orderAmount = json.getJSONObject("order_amount");
			if(orderAmount!=null&&!orderAmount.isEmpty()&&orderAmount.toString().length()>2){
				pd.put("orderAmount_amount",DoubleUtil.getRes(orderAmount.getDouble("cent"),100.00));
				pd.put("orderAmount_cent", orderAmount.get("cent"));
				pd.put("orderAmount_currencyCode", orderAmount.get("currency_code"));
			}else{
				pd.put("orderAmount_amount", null);
				pd.put("orderAmount_cent", null);
				pd.put("orderAmount_currencyCode",null);
			}
			JSONObject refundInfo = json.getJSONObject("refund_info");
			if(refundInfo!=null&&!refundInfo.isEmpty()&&refundInfo.toString().length()>2){
				if(refundInfo.get("refund_reason")!=null){
					pd.put("refundInfo_refundReason", refundInfo.get("refund_reason").toString().replaceAll("\\xAC\\xED\\x00\\x05", ""));
				}else{
					pd.put("refundInfo_refundReason",null);
				}				
				pd.put("refundInfo_refundStatus", refundInfo.get("refund_status"));
				pd.put("refundInfo_refundType", refundInfo.get("refund_type"));
			}else {
				pd.put("refundInfo_refundReason",null);
				pd.put("refundInfo_refundStatus", null);
				pd.put("refundInfo_refundType", null);
			}
			pd.put("leftSendGoodDay", dto.get("left_send_good_day"));
			pd.put("leftSendGoodHour", dto.get("left_send_good_hour"));
			pd.put("leftSendGoodMin", dto.get("left_send_good_min"));
			pd.put("memo", json.get("memo"));
			pd.put("MODIFY_DATE", new Date());
			//orderService.edit(pd);
			updateOrderList.add(pd);
			editOther(json,dto);			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("抛出异常信息：",e);
		}
	}
	public void save(PageData store,JSONObject json,JSONObject  dto)  {
		try {
		PageData pd = new PageData();
		pd.put("buyerSignerFullname", json.get("buyer_signer_fullname")==null?"":json.get("buyer_signer_fullname"));
		pd.put("buyerloginid", json.get("buyerloginid")==null?"":json.get("buyerloginid"));
		pd.put("frozenStatus", json.get("frozen_status")==null?"":json.get("frozen_status"));
		pd.put("fundStatus", json.get("fund_status")==null?"":json.get("fund_status"));
		if(json.get("gmt_create")!=null){
			pd.put("gmtCreate", json.get("gmt_create"));
		}else{
			pd.put("gmtCreate", null);
		}
		if(json.get("gmt_modified")!=null){
			pd.put("gmtModified", json.get("gmt_modified"));
		}else{
			pd.put("gmtModified", null);
		}
		if(json.get("gmt_pay_success")!=null){
			pd.put("gmtPaySuccess", json.get("gmt_pay_success"));
		}else{
			pd.put("gmtPaySuccess", null);
		}
		if(json.get("gmt_trade_end")!=null){
			pd.put("gmtTradeEnd", json.get("gmt_trade_end"));
		}else {
			pd.put("gmtTradeEnd", null);
		}
		pd.put("orderId", json.get("id"));
		pd.put("isPhone", json.get("is_phone")==null?"":json.get("is_phone"));
		pd.put("issueStatus", json.get("issue_status")==null?"":json.get("issue_status"));
		pd.put("loanStatus", json.get("loan_status")==null?"":json.get("loan_status"));
		pd.put("logisticsStatus", json.get("logistics_status")==null?"":json.get("logistics_status"));
		pd.put("orderStatus", json.get("order_status")==null?"":json.get("order_status"));
		pd.put("paymentType", json.get("payment_type")==null?"":json.get("payment_type"));
		if(json.get("seller_operator_aliidloginid")!=null){
			pd.put("sellerOperatorAliidloginid", json.get("seller_operator_aliidloginid"));
		}else{
			pd.put("sellerOperatorAliidloginid", null);
		}
		
		pd.put("sellerOperatorLoginId", json.get("seller_operator_login_id"));
		pd.put("sellerSignerFullname", json.get("seller_signer_fullname"));
		JSONObject escrowFee = json.getJSONObject("escrow_fee");
		if(escrowFee!=null&&!escrowFee.isEmpty()&&escrowFee.toString().length()>2){
			pd.put("escrowFee_amount", escrowFee.get("amount"));
			pd.put("escrowFee_cent", escrowFee.get("cent"));
			pd.put("escrowFee_currencyCode", escrowFee.get("currency_code"));
		}else{
			pd.put("escrowFee_amount", null);
			pd.put("escrowFee_cent", null);
			pd.put("escrowFee_currencyCode", null);
		}
		JSONObject initOderAmount = json.getJSONObject("init_oder_amount");
		if(initOderAmount!=null&&!initOderAmount.isEmpty()&&initOderAmount.toString().length()>2){
			pd.put("initOderAmount_amount", DoubleUtil.getRes(initOderAmount.getDouble("cent"),100.00));
			pd.put("initOderAmount_cent", initOderAmount.get("cent"));
			pd.put("initOderAmount_currencyCode", initOderAmount.get("currency_code"));
		}else{
			pd.put("initOderAmount_amount", null);
			pd.put("initOderAmount_cent", null);
			pd.put("initOderAmount_currencyCode", null);
		}
		JSONObject loanInfo = json.getJSONObject("loan_info");
		if(loanInfo!=null&&!loanInfo.isEmpty()&&loanInfo.toString().length()>2){
			if(json.get("loanTime")!=null){
				pd.put("loanTime", ExpressDateUtil.parse(json.get("loan_time")+""));
			}else{
				pd.put("loanTime", null);
			}
			JSONObject loanAmount = loanInfo.getJSONObject("loan_amount");
			if(initOderAmount!=null&&!initOderAmount.isEmpty()&&initOderAmount.toString().length()>2){
				pd.put("loanAmount_amount", DoubleUtil.getRes(loanAmount.getDouble("cent"),100.00));
				pd.put("loanAmount_cent", loanAmount.get("cent"));
				pd.put("loanAmount_currencyCode", loanAmount.get("currency_code"));
			}else{
				pd.put("loanAmount_amount", null);
				pd.put("loanAmount_cent", null);
				pd.put("loanAmount_currencyCode", null);
			}
		}else {
			pd.put("loanTime", null);
			pd.put("loanAmount_amount", null);
			pd.put("loanAmount_cent", null);
			pd.put("loanAmount_currencyCode", null);
		}
		JSONObject logisticsAmount = json.getJSONObject("logistics_amount");
		if(logisticsAmount!=null&&!logisticsAmount.isEmpty()&&logisticsAmount.toString().length()>2){
			pd.put("logisticsAmount_amount", logisticsAmount.get("amount"));
			pd.put("logisticsAmount_cent", logisticsAmount.get("cent"));
			pd.put("logisticsAmount_currencyCode", logisticsAmount.get("currency_code"));
		}else{
			pd.put("logisticsAmount_amount", null);
			pd.put("logisticsAmount_cent", null);
			pd.put("logisticsAmount_currencyCode", null);
		}
		JSONObject orderAmount = json.getJSONObject("order_amount");
		if(orderAmount!=null&&!orderAmount.isEmpty()&&orderAmount.toString().length()>2){
			pd.put("orderAmount_amount", DoubleUtil.getRes(orderAmount.getDouble("cent"),100.00));
			pd.put("orderAmount_cent", orderAmount.get("cent"));
			pd.put("orderAmount_currencyCode", orderAmount.get("currency_code"));
		}else {
			pd.put("orderAmount_amount", null);
			pd.put("orderAmount_cent", null);
			pd.put("orderAmount_currencyCode", null);
		}
		JSONObject refundInfo = json.getJSONObject("refund_info");
		if(refundInfo!=null&&!refundInfo.isEmpty()&&refundInfo.toString().length()>2){
			if(refundInfo.get("refund_reason")!=null){
				pd.put("refundInfo_refundReason", refundInfo.get("refund_reason").toString().replaceAll("\\xAC\\xED\\x00\\x05", ""));
			}else{
				pd.put("refundInfo_refundReason",null);
			}
			pd.put("refundInfo_refundStatus", refundInfo.get("refund_status"));
			pd.put("refundInfo_refundType", refundInfo.get("refund_type"));
		}else {
			pd.put("refundInfo_refundReason",null);
			pd.put("refundInfo_refundStatus", null);
			pd.put("refundInfo_refundType", null);
		}
		if(json.get("memo")!=null&&json.get("memo").toString().length()>2){
			pd.put("memo", json.get("memo").toString());
		}else{
			pd.put("memo", null);
		}
		
		pd.put("leftSendGoodDay", dto.get("left_send_good_day"));
		pd.put("leftSendGoodHour", dto.get("left_send_good_hour"));
		pd.put("leftSendGoodMin", dto.get("left_send_good_min"));
		pd.put("STORE_ID", store.get("ID"));
		pd.put("CREATE_DATE", new Date());
		pd.put("MODIFY_DATE", new Date());
		orderList.add(pd);
		saveOther(json,dto);		
		} catch (Exception e) {
			e.printStackTrace();
			log.error("抛出异常信息：",e);
		}
		
	}
	
	public void editOther(JSONObject json,JSONObject dto){
		try {
		PageData pd = new PageData();
		pd.put("orderId", json.get("id"));	
		logisticinfoDel.add(json.get("id")+"");
		//保存物流信息
			JSONArray logisticInfoList = new JSONArray();
		if(json.containsKey("logistic_info_list")&&json.getJSONObject("logistic_info_list").toString().length()>3){
			logisticInfoList = json.getJSONObject("logistic_info_list").getJSONArray("aeop_tp_logistic_info_dto");
		}

		for(int i=0;i<logisticInfoList.size();i++){
			JSONObject oo = logisticInfoList.getJSONObject(i);
			PageData obj = new PageData();
			obj.put("orderId", json.get("id"));
			if(oo.get("gmt_received")!=null){
				obj.put("gmtReceived", oo.get("gmt_received"));
			}else{
				obj.put("gmtReceived",null);
			}
			if(oo.get("gmt_send")!=null){
				obj.put("gmtSend", oo.get("gmt_send"));
			}else {
				obj.put("gmtSend",null);
			}
			if(oo.get("have_tracking_info")!=null){
				obj.put("haveTrackingInfo", oo.get("have_tracking_info"));
			}else{
				obj.put("haveTrackingInfo",null);
			}
			
			obj.put("logisticsNo", oo.get("logistics_no"));
			obj.put("logisticsServiceName", oo.get("logistics_service_name"));
			obj.put("logisticsTypeCode", oo.get("logistics_type_code"));
			obj.put("receiveStatus", oo.get("receive_status"));
			if(oo.get("recv_status_desc")!=null){
				obj.put("recvStatusDesc", oo.get("recv_status_desc"));
			}else{
				obj.put("recvStatusDesc", null);
			}
			if(oo.get("ship_order_id")!=null){
				obj.put("shipOrderId", oo.get("ship_order_id"));
			}else{
				obj.put("shipOrderId", null);
			}
			obj.put("CREATE_DATE", new Date());
			logisticinfos.add(obj);
		}
				
		} catch (Exception e) {
			e.printStackTrace();
			log.error("抛出异常信息：",e);
		}
	}
	public void saveOther(JSONObject json,JSONObject dto){
		try {
		//保存卖家信息
		PageData pd = new PageData();
		pd.put("orderId", json.get("id"));
		buyerinfoDel.add(json.get("id")+"");
		receiptaddressDel.add(json.get("id")+"");
		childDel.add(json.get("id")+"");
		logisticinfoDel.add(json.get("id")+"");
		msgDel.add(json.get("id")+"");
		productDel.add(json.get("id")+"");
		JSONObject buyerInfo = json.getJSONObject("buyer_info");
		if(buyerInfo!=null && !buyerInfo.isEmpty()&&buyerInfo.toString().length()>2 ){
			PageData obj = new PageData();
			obj.put("orderId", json.get("id"));
			obj.put("country", buyerInfo.get("country")==null?"":buyerInfo.get("country"));
			obj.put("firstName", buyerInfo.get("first_name")==null?"":buyerInfo.get("first_name"));
			obj.put("lastName", buyerInfo.get("last_name")==null?"":buyerInfo.get("last_name"));
			obj.put("loginId", buyerInfo.get("login_id")==null?"":buyerInfo.get("login_id"));
			//obj.put("Email", buyerInfo.get("Email")==null?"":buyerInfo.get("Email"));
			obj.put("CREATE_DATE", new Date());
			buyerinfoList.add(obj);
		}
		//保存地址
		JSONObject receiptAddress = json.getJSONObject("receipt_address");
		if(receiptAddress!=null && !receiptAddress.isEmpty()&&receiptAddress.toString().length()>2 ){
			PageData obj = new PageData();
			obj.put("orderId", json.get("id"));
			obj.put("address", receiptAddress.get("address"));
			obj.put("address2", receiptAddress.get("address2"));
			obj.put("city", receiptAddress.get("city"));
			obj.put("contactPerson", receiptAddress.get("contact_person"));
			obj.put("country", receiptAddress.get("country"));
			if(receiptAddress.get("detail_address")!=null && receiptAddress.get("detail_address").toString().length()>0){
				obj.put("detailAddress",receiptAddress.get("detail_address").toString().replaceAll("\\xAC\\xED\\x00\\x05", ""));
			}else{
				obj.put("detailAddress", null);
			}
			obj.put("faxArea", receiptAddress.get("fax_area"));
			obj.put("faxCountry", receiptAddress.get("fax_country"));
			obj.put("faxNumber", receiptAddress.get("fax_number"));
			obj.put("mobileNo", receiptAddress.get("mobile_no"));
			obj.put("phoneArea", receiptAddress.get("phone_area"));
			obj.put("phoneCountry", receiptAddress.get("phone_country"));
			obj.put("phoneNumber", receiptAddress.get("phone_number"));
			obj.put("province", receiptAddress.get("province"));
			obj.put("zip", receiptAddress.get("zip"));
			obj.put("CREATE_DATE", new Date());
			receiptaddressList.add(obj);
		}
		
		//保存子订单信息
			JSONArray childOrderList = new JSONArray();
		if(json.containsKey("child_order_list")&&json.getJSONObject("child_order_list").toString().length()>3){
			 childOrderList = json.getJSONObject("child_order_list").getJSONArray("aeop_tp_child_order_dto");
		}
		if(childOrderList!=null && !childOrderList.isEmpty()&&childOrderList.toString().length()>2 ){
			for(int i=0;i<childOrderList.size();i++){
				JSONObject oo = childOrderList.getJSONObject(i);
				PageData pro = new PageData();
				pro.put("orderId", json.get("id"));
				pro.put("frozenStatus", oo.get("frozen_status"));
				pro.put("fundStatus", oo.get("fund_status"));
				pro.put("childOrderId", oo.get("id"));
				pro.put("issueStatus", oo.get("issue_status"));
				pro.put("lotNum", oo.get("lot_num"));
				pro.put("orderStatus", oo.get("order_status"));
				String pId = "";
				String pValueId ="";
				JSONObject attr = oo.getJSONObject("product_attributes");
				if(attr!=null && !attr.isEmpty() && attr.toString().length()>10){
					JSONArray skus = attr.getJSONArray("sku");
					for(int j=0;j<skus.size();j++){
						if(j==skus.size()-1){
							JSONObject sku = skus.getJSONObject(j);
							pId = pId + sku.getInt("pId")+":";
							pId = pId + sku.getInt("pValueId")+"";
						}else{
							JSONObject sku = skus.getJSONObject(j);
							pId = pId + sku.getInt("pId")+":";
							pId = pId + sku.getInt("pValueId")+";";
						}
						
					}

				}
				pro.put("productAttributes", oo.get("product_attributes").toString());
				pro.put("productCount", oo.get("product_count"));
				pro.put("productId", oo.get("product_id"));
				pro.put("productName", oo.get("product_name"));
				pro.put("productStandard", oo.get("product_standard"));
				pro.put("productUnit", oo.get("product_unit"));
				if(pId.length()>0){
					pro.put("skuCode", oo.get("product_id")+"-"+pId);
				}else{
					pro.put("skuCode", oo.get("product_id"));
				}				
				JSONObject initOrderAmt = oo.getJSONObject("init_order_amt");
				if(initOrderAmt!=null&&!initOrderAmt.isEmpty()&&initOrderAmt.toString().length()>2){
					pro.put("initOrderAmt_amount", DoubleUtil.getRes(initOrderAmt.getDouble("cent"),100.00));
					pro.put("initOrderAmt_cent", initOrderAmt.get("cent"));
				}else{
					pro.put("initOrderAmt_amount", null);
					pro.put("initOrderAmt_cent", null);
				}
				JSONObject productPrice = oo.getJSONObject("product_price");
				if(productPrice!=null&&!productPrice.isEmpty()&&productPrice.toString().length()>2){
					pro.put("productPrie_amount", DoubleUtil.getRes(productPrice.getDouble("cent"),100.00));
					pro.put("productPrie_cent", productPrice.get("cent"));
					pro.put("productPrie_currencyCode", productPrice.get("currency_code"));
					pro.put("initOrderAmt_currencyCode", productPrice.get("currency_code"));
				}else{
					pro.put("productPrie_amount", null);
					pro.put("productPrie_cent", null);
					pro.put("productPrie_currencyCode", null);
					pro.put("initOrderAmt_currencyCode", null);
				}
				pro.put("logisticsType", oo.get("logistics_type"));
				pro.put("CREATE_DATE", new Date());
				childList.add(pro);
			}
		}		
		//保存物流信息
		JSONArray logisticInfoList = new JSONArray();
		if(json.containsKey("logistic_info_list")&&json.getJSONObject("logistic_info_list").toString().length()>3){
			 logisticInfoList = json.getJSONObject("logistic_info_list").getJSONArray("aeop_tp_logistic_info_dto");
		}
		for(int i=0;i<logisticInfoList.size();i++){
			JSONObject oo = logisticInfoList.getJSONObject(i);
			PageData obj = new PageData();
			obj.put("orderId", json.get("id"));
			if(oo.get("gmt_received")!=null){
				obj.put("gmtReceived", oo.get("gmt_received"));
			}else{
				obj.put("gmtReceived", null);
			}
			if(oo.get("gmt_send")!=null){
				obj.put("gmtSend",oo.get("gmt_send"));
			}else{
				obj.put("gmtSend",null);
			}
			if(oo.get("have_tracking_info")!=null){
				obj.put("haveTrackingInfo", oo.get("have_tracking_info"));
			}else{
				obj.put("haveTrackingInfo",null);
			}
			
			obj.put("logisticsNo", oo.get("logistics_no")==null?"":oo.get("logistics_no"));
			obj.put("logisticsServiceName", oo.get("logistics_service_name")==null?"":oo.get("logistics_service_name"));
			obj.put("logisticsTypeCode", oo.get("logistics_type_code")==null?"":oo.get("logistics_type_code"));
			if(oo.get("recv_status_desc")!=null){
				obj.put("recvStatusDesc", oo.get("recv_status_desc"));
			}else{
				obj.put("recvStatusDesc", null);
			}
			if(oo.get("ship_order_id")!=null){
				obj.put("shipOrderId", oo.get("ship_order_id"));
			}else{
				obj.put("shipOrderId", null);
			}
			obj.put("receiveStatus", oo.get("receive_status")==null?"":oo.get("receive_status"));
			obj.put("CREATE_DATE", new Date());
			//log.info("obj----------------"+obj);
			logisticinfos.add(obj);
		}
		//保存留言信息
			JSONArray orderMsgList = new JSONArray();
		if(json.containsKey("order_msg_list")&& json.getJSONObject("order_msg_list").toString().length()>3){
			 orderMsgList = json.getJSONObject("order_msg_list").getJSONArray("aeop_tp_order_msg_dto");
		}

		for(int i=0;i<orderMsgList.size();i++){
			JSONObject oo = orderMsgList.getJSONObject(i);
			PageData obj = new PageData();
			obj.put("orderId", json.get("id"));
			obj.put("content", oo.get("content"));
			if(oo.get("gmt_create")!=null){
				obj.put("gmtCreate", oo.get("gmt_create"));
			}else{
				obj.put("gmtCreate",null);
			}
			if(oo.get("gmt_modified")!=null){
				obj.put("gmtModified", oo.get("gmt_modified"));
			}else{
				obj.put("gmtModified",null);
			}
			obj.put("msgId", oo.get("id"));
			obj.put("poster", oo.get("poster"));
			obj.put("receiverAdminSeq", oo.get("receiver_admin_seq"));
			obj.put("receiverFirstName", oo.get("receiver_first_name"));
			obj.put("receiverLastName", oo.get("receiver_last_name"));
			obj.put("receiverLoginId", oo.get("receiver_login_id"));
			obj.put("receiverSeq", oo.get("receiver_seq"));
			obj.put("senderAdminSeq", oo.get("sender_admin_seq"));
			obj.put("senderFirstName", oo.get("sender_first_name"));
			obj.put("senderLastName", oo.get("sender_last_name"));
			obj.put("senderLoginId", oo.get("sender_login_id"));
			obj.put("senderSeq", oo.get("sender_seq"));
			obj.put("status", oo.get("status"));
			obj.put("CREATE_DATE", new Date());
			msgList.add(obj);
		}
		//保存商品信息
			JSONArray childOrderExtInfoList = new JSONArray();
		if(json.containsKey("child_order_ext_info_list")&&json.getJSONObject("child_order_ext_info_list").toString().length()>3){
			 childOrderExtInfoList = json.getJSONObject("child_order_ext_info_list").getJSONArray("aeop_tp_order_product_info_dto");
		}
			JSONArray OrderProductVO = new JSONArray();
		if(dto.containsKey("product_list")){
			 OrderProductVO = dto.getJSONObject("product_list").getJSONArray("order_product_vo");
		}

		List<PageData> pds = new ArrayList<>();
		for(int i=0;i<OrderProductVO.size();i++){
			JSONObject oa = OrderProductVO.getJSONObject(i);
			PageData obj = new PageData();
			obj.put("productId", oa.get("product_id"));
			obj.put("buyerSignerFirstName", oa.get("buyer_signer_first_name"));
			obj.put("buyerSignerLastName", oa.get("buyer_signer_last_name"));
			obj.put("canSubmitIssue", oa.get("can_submit_issue"));
			obj.put("childId", oa.get("child_id"));
			obj.put("deliveryTime", oa.get("delivery_time"));
			obj.put("freightCommitDay", oa.get("freight_commit_day"));
			obj.put("fundStatus", oa.get("fund_status"));
			obj.put("goodsPrepareTime", oa.get("goods_prepare_time"));
			obj.put("issueMode", oa.get("issue_mode"));
			obj.put("issueStatus", oa.get("issue_status"));
			obj.put("logisticsServiceName", oa.get("logistics_service_name"));
			obj.put("logisticsType", oa.get("logistics_type"));
			obj.put("memo", oa.get("memo"));
			obj.put("moneyBack3x", oa.get("money_back3x"));
			obj.put("productCount", oa.get("product_count"));
			obj.put("productImgUrl", oa.get("product_img_url"));
			obj.put("productSnapUrl", oa.get("product_snap_url"));
			obj.put("productStandard", oa.get("product_standard"));
			obj.put("productUnit", oa.get("product_unit"));
			if(oa.get("send_goods_time")!=null){
				obj.put("sendGoodsTime",oa.get("send_goods_time"));
			}else{
				obj.put("sendGoodsTime",null);
			}
			obj.put("showStatus", oa.get("show_status"));
			obj.put("skuCode", oa.get("sku_code"));
			obj.put("sonOrderStatus", oa.get("son_order_status"));
			JSONObject logisticsAmount = oa.getJSONObject("logistics_amount");
			if(logisticsAmount!=null&&!logisticsAmount.isEmpty()&&logisticsAmount.toString().length()>2){
				obj.put("logisticsAmount_amount", logisticsAmount.get("amount"));
				obj.put("logisticsAmount_cent", logisticsAmount.get("cent"));
				obj.put("logisticsAmount_currencyCode", logisticsAmount.get("currency_code"));
			}else{
				obj.put("logisticsAmount_amount", null);
				obj.put("logisticsAmount_cent", null);
				obj.put("logisticsAmount_currencyCode", null);
			}
			JSONObject totalProductAmount = oa.getJSONObject("total_product_amount");
			if(totalProductAmount!=null&&!totalProductAmount.isEmpty()&&totalProductAmount.toString().length()>2){
				obj.put("totalProductAmount_amount", DoubleUtil.getRes(totalProductAmount.getDouble("cent"),100.00));
				obj.put("totalProductAmount_cent", totalProductAmount.get("cent"));
				obj.put("totalProductAmount_currencyCode", totalProductAmount.get("currency_code"));
			}else{
				obj.put("totalProductAmount_amount", null);
				obj.put("totalProductAmount_cent", null);
				obj.put("totalProductAmount_currencyCode", null);
			}
			JSONObject productUnitPrice = oa.getJSONObject("product_unit_price");
			if(productUnitPrice!=null&&!productUnitPrice.isEmpty()&&productUnitPrice.toString().length()>2){
				obj.put("productUnitPrice_amount", DoubleUtil.getRes(productUnitPrice.getDouble("cent"),100.00));
				obj.put("productUnitPrice_cent", productUnitPrice.get("cent"));
				obj.put("productUnitPrice_currencyCode", productUnitPrice.get("currency_code"));
			}else{
				obj.put("productUnitPrice_amount", null);
				obj.put("productUnitPrice_cent", null);
				obj.put("productUnitPrice_currencyCode", null);
			}
			pds.add(obj);
		}
		for(int i=0;i<childOrderExtInfoList.size();i++){
			JSONObject oo = childOrderExtInfoList.getJSONObject(i);			
			PageData obj = new PageData();
			obj.put("orderId", json.get("id"));
			obj.put("productId", oo.get("product_id"));
			obj.put("productName", oo.get("product_name"));
			obj.put("quantity", oo.get("quantity"));
			obj.put("sku", oo.get("sku").toString());
			obj.put("unitPrice", oo.get("unit_price"));
			JSONObject unitPrice = oo.getJSONObject("unit_price");
			if(unitPrice!=null&&!unitPrice.isEmpty()&&unitPrice.toString().length()>2){
				obj.put("amount", DoubleUtil.getRes(unitPrice.getDouble("cent"),100.00));
				obj.put("cent", unitPrice.get("cent"));
				obj.put("currencyCode", unitPrice.get("currency_code"));
			}else{
				obj.put("amount", null);
				obj.put("cent", null);
				obj.put("currencyCode", null);
			}
			for(int j=0;j<OrderProductVO.size();j++){
				JSONObject oa = OrderProductVO.getJSONObject(j);
				if(obj.get("productId").equals(oa.get("product_id"))){
					pds.get(j).remove("productId");
					obj.putAll(pds.get(j));
				}
			}
			obj.put("CREATE_DATE", new Date());
			productList.add(obj);
		}		
		} catch (Exception e) {
			e.printStackTrace();
			log.error("抛出异常信息：",e);
		}
	}

	
	
	
}
